Skip to content

Delete Z3's unpacked native libraries once they are loaded - #541

Merged
CatarinaGamboa merged 1 commit into
mainfrom
fix/535-z3-temp-leak
Nov 23, 2026
Merged

CatarinaGamboa merged 1 commit into
mainfrom
fix/535-z3-temp-leak

Conversation

@CatarinaGamboa

@CatarinaGamboa CatarinaGamboa commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #535

Cause

tools.aqua:z3-turnkey 4.8.17 loads Z3 in the static initializer of com.microsoft.z3.Native, which calls Z3Loader.loadZ3() without any condition. That method makes a fresh ${java.io.tmpdir}/z3-turnkey<random> folder, unpacks libz3.so and libz3java.so into it (about 30 MB), marks them deleteOnExit and System.loads them. deleteOnExit only runs on a normal JVM exit, so every verifier JVM that is killed (language server, MCP server, timed-out test run) leaves its folder behind.

Fix

Once Z3 is loaded, the unpacked files are no longer needed: on Linux a loaded library stays mapped and usable after its file is deleted. The first TranslatorToZ3 (the only place that creates a Z3 Context) now calls Z3NativeLibraries.deleteUnpackedFiles(). It reads /proc/self/maps, takes the mapped files whose folder starts with z3-turnkey, deletes them and then their folder. The z3-turnkey deleteOnExit hooks later find nothing to delete, which is harmless.

It only deletes files that this process has mapped, so the folder of another running verifier is never touched, and there is no race with JVMs that are still unpacking. If /proc/self/maps is missing (macOS, Windows) or a delete fails, nothing changes: the folder is still removed at a normal exit, as today. The only window left on Linux is the few milliseconds between unpacking and the first TranslatorToZ3.

Alternatives considered

  • Newer z3-turnkey. No. The latest releases (4.12.x, 4.14.1, 5.3.3) load through tools.aqua:turnkey-support 1.0.0 (the only version), and its TurnKey loader still uses Files.createTempDirectory + deleteOnExit. A bump would change solver behaviour and not fix the leak.
  • Our own loader before Z3 is first touched (unpack, System.load, delete). Not enough on its own: Native's static initializer always calls Z3Loader.loadZ3(), and there is no system property to turn it off, so z3-turnkey would still unpack (and leak) a second copy. Avoiding that would mean shadowing com.microsoft.z3.Z3Loader with our own class on the classpath, which depends on classpath order and is fragile.
  • Redirecting java.io.tmpdir just for the load. Not possible: the JDK reads the temp directory once at startup.
  • Sweeping stale z3-turnkey* folders at startup. Not done. With several verifiers running at once, it is hard to tell a leftover from a folder another JVM is unpacking right now. With this fix, Linux JVMs no longer leave folders, so there is little left to sweep. Folders that leaked before this fix still need a one-time rm -rf /tmp/z3-turnkey* while no verifier runs.

Verification

  • Manual SIGKILL check: ran CommandLineLauncher on liquidjava-example/src/main/java/testSuite with a private -Djava.io.tmpdir, waited until Z3 was mapped and solving, then kill -9:
    • this branch: the process maps .../z3-turnkey2988981119362764953/libz3.so (deleted) and keeps verifying; after the kill the tmpdir is empty (4 KB).
    • released 0.0.38: after the kill, z3-turnkey12893544316004404040/ with libz3.so + libz3java.so is left behind (30 MB).
  • mvn -q test from the repo root passes, and leaves nothing in its private tmpdir.

🤖 Generated with Claude Code

@CatarinaGamboa

Copy link
Copy Markdown
Collaborator Author

Codex review, round 1

Prompt: review git diff origin/main...HEAD for correctness, races (concurrent verifier JVMs, threads, class loaders), platform issues (Linux, macOS, Windows, containers) and test reliability, with concrete failure scenarios.

Result:

No relevant findings. The diff's Linux cleanup matches z3-turnkey 4.8.17's extraction and load sequence; I found no concrete failure scenario in the requested race or platform cases. I reviewed the code and dependency bytecode without modifying files or running tests.

Nothing to fix, so the Codex loop stops after this round.

@CatarinaGamboa

Copy link
Copy Markdown
Collaborator Author

Clean-code review (clean-code-principles skill, only the code this PR adds)

Fixed (f707a83):

  • Z3NativeLibraries.java:23 - [name-meaningful] The flag deleted was set before the deletion ran and stayed set when a delete failed. Renamed it to attempted, which is what it records.

Declined:

  • Z3NativeLibrariesTest.java - [core-dry] The test repeats the /z3-turnkey string and its own /proc/self/maps parsing instead of reusing Z3NativeLibraries.unpackedLibraries. This is on purpose: if the test reused the production parser, a parser that found nothing would make the test pass without testing anything.
  • TranslatorToZ3.java:69 - [temporal coupling] The call depends on Java running field initializers (z3 = new Context(), which loads Z3) before the constructor body. The inline comment says so. Moving it would mean a separate factory or a static initializer that forces Z3 to load, which is more code than a one-line call in a small PR.
  • Z3NativeLibraries.java:42 - [core-fail-fast] The IOException is swallowed. On purpose: the cleanup is best-effort, it must never stop verification, and if it fails the files are still removed at a normal exit, as before this PR. The comment explains this.
  • [solid-srp] The new class has one job (find and delete this process's unpacked Z3 files). Nothing to change.

z3-turnkey unpacks libz3 into a fresh temp folder per JVM and removes it
only on a normal exit, so every killed verifier leaked about 30 MB. Once
the first Z3 context exists, delete the files this process has mapped
(read from /proc/self/maps) and their folder; on Linux a loaded library
stays usable after its file is deleted. Elsewhere nothing changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CatarinaGamboa
CatarinaGamboa merged commit 76ed603 into main Nov 23, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Killed verifier JVMs leave ~30 MB of Z3 native libraries in /tmp

1 participant